<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="一、音量调节QT的音量调节是通过void QAudioOutput::setVolume(qreal volume) 方法实现的，所以结合QSlider，可以这样写：12345// slider的范围为0-100， volume的范围为0-1connect(ui-&amp;gt;volumSlider, &amp;">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="FFMpeg QT跨平台播放器五 音量调节和seek操作">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="一、音量调节QT的音量调节是通过void QAudioOutput::setVolume(qreal volume) 方法实现的，所以结合QSlider，可以这样写：12345// slider的范围为0-100， volume的范围为0-1connect(ui-&amp;gt;volumSlider, &amp;">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>FFMpeg QT跨平台播放器五 音量调节和seek操作 - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>FFMpeg QT跨平台播放器五 音量调节和seek操作</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2019-09-18
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        


<a href="/blog/tags/播放器/">#播放器</a>


                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                        

<a href="/blog/categories/音视频/">音视频</a>

                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <h3 id="一、音量调节"><a href="#一、音量调节" class="headerlink" title="一、音量调节"></a>一、音量调节</h3><p>QT的音量调节是通过<strong>void QAudioOutput::setVolume(qreal volume)</strong> 方法实现的，所以结合QSlider，可以这样写：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// slider的范围为0-100， volume的范围为0-1</span><br><span class="line">connect(ui-&gt;volumSlider, &amp;QSlider::valueChanged,</span><br><span class="line">            [=](int value) &#123;</span><br><span class="line">                audioOut-&gt;setVolume(value / 100.0);</span><br><span class="line">    &#125;);</span><br></pre></td></tr></table></figure></p>
<h3 id="二、seek"><a href="#二、seek" class="headerlink" title="二、seek"></a>二、seek</h3><p>FFMpeg的seek主要是能过 <strong>int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags); </strong>实现。算出seek时间相对于音频/视频TimeBase的值并调用即可；<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">void MPMovieDecoder::setPosition(float seconds)</span><br><span class="line">&#123;</span><br><span class="line">    _position = seconds;</span><br><span class="line">    isEOF = false;</span><br><span class="line"></span><br><span class="line">    if(_videoStream != -1) &#123;</span><br><span class="line">        int64_t ts = (int64_t)(seconds / _videoTimeBase);</span><br><span class="line">        avformat_seek_file(_formatCtx, _videoStream, ts, ts, ts, AVSEEK_FLAG_FRAME);</span><br><span class="line">        avcodec_flush_buffers(_videoCodecCtx);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (_audioStream != -1) &#123;</span><br><span class="line">        int64_t ts = (int64_t)(seconds / _audioTimeBase);</span><br><span class="line">        avformat_seek_file(_formatCtx, _audioStream, ts, ts, ts, AVSEEK_FLAG_FRAME);</span><br><span class="line">        avcodec_flush_buffers(_audioCodecCtx);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>对于seek，我们通常会与slider配合使用，所以可以这样写：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 准备拉slider的时候，停止播放</span><br><span class="line">connect(ui-&gt;processSlider, &amp;QSlider::sliderPressed, this, &amp;MainWindow::onProcessSliderPress);</span><br><span class="line"></span><br><span class="line">// 这里不用valueChange信号，主要是因为会与播放进度显示冲突</span><br><span class="line">    connect(ui-&gt;processSlider, &amp;QSlider::sliderReleased, this, &amp;MainWindow::onProcessSliderRelease);</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 停止播放并记下当前是播放还是暂停，决定滑动后的状态</span><br><span class="line">void MainWindow::onProcessSliderPress()</span><br><span class="line">&#123;</span><br><span class="line">    playMode = playing;</span><br><span class="line">    playing = false;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">void MainWindow::onProcessSliderRelease()</span><br><span class="line">&#123;</span><br><span class="line">	//获取slider的值，计算相对于文件总时长的时间，这里是之前停止播放有关，不停止播放，更新播放进度会回跳</span><br><span class="line">    float total = decoder-&gt;getDuration();</span><br><span class="line">    float position = total * ui-&gt;processSlider-&gt;value() / 100.0;</span><br><span class="line">    setMoviePositon(position);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void MainWindow::setMoviePositon(float seconds)</span><br><span class="line">&#123;</span><br><span class="line">    m_processTimer = new QTimer(this);</span><br><span class="line">    connect(m_processTimer, &amp;QTimer::timeout,</span><br><span class="line">            [=]()&#123;</span><br><span class="line">        updatePosition(seconds, playMode);</span><br><span class="line">    &#125;);</span><br><span class="line">    m_processTimer-&gt;start(100);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void MainWindow::updatePosition(float position, bool playMode)</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    if(m_processTimer-&gt;isActive()) &#123;</span><br><span class="line">        m_processTimer-&gt;stop();</span><br><span class="line">    &#125;</span><br><span class="line">    if (m_processTimer != NULL) &#123;</span><br><span class="line">        delete  m_processTimer;</span><br><span class="line">        m_processTimer = NULL;</span><br><span class="line">    &#125;</span><br><span class="line">	// 清除缓存的数据</span><br><span class="line">    freeBufferedFrames();</span><br><span class="line">    float tmp = position &gt; 0 ? position : 0;</span><br><span class="line">    position = (decoder-&gt;getDuration() - 1 ) &gt; tmp ? tmp : decoder-&gt;getDuration() - 1;</span><br><span class="line">    qDebug() &lt;&lt; &quot;updatePosition &quot; &lt;&lt; position &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    if(playMode) &#123;</span><br><span class="line">        decoder-&gt;setPosition(position);</span><br><span class="line">        _moviePosition = decoder-&gt;getPosition();</span><br><span class="line">        updateCurrentTime();</span><br><span class="line">        play();</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        decoder-&gt;setPosition(position);</span><br><span class="line">        decodeFrames();</span><br><span class="line">        _moviePosition = decoder-&gt;getPosition();</span><br><span class="line">        updateCurrentTime();</span><br><span class="line">        presentFrame();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void MainWindow::updateCurrentTime()</span><br><span class="line">&#123;</span><br><span class="line">    ui-&gt;currentTime-&gt;setText(formatTime((int)_moviePosition));</span><br><span class="line">    int value = (_moviePosition / (decoder-&gt;getDuration()) * 100);</span><br><span class="line">    ui-&gt;processSlider-&gt;setValue(value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>